/*
 * This project provides a number of implementations for indexing and evaluating
 * <i>Continuous Top-k Queries over Text Streams</i>
 * and has been developped as part of the work described in the article:
 * 
 * Vouzoukidou, N., Amann, B., & Christophides, V. (2012, October). 
 * Processing continuous text queries featuring non-homogeneous scoring functions.
 * In Proceedings of the 21st ACM international conference on Information and 
 * knowledge management (pp. 1065-1074). ACM.
 * http://dl.acm.org/citation.cfm?id=2398404
 * 
 * This software is distributed under the GNU General Public License and as an
 * additional condition of usage, please cite the aforementioned article.
 * 
 * Copyright (C) 2012 ICS/FORTH, Greece and 
 *               2012 LIP6, France
 * 
 * Author: Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package examples;

import gr.forth.ics.continuoustopk.Item;
import gr.forth.ics.continuoustopk.Query;
import gr.forth.ics.continuoustopk.Term;
import gr.forth.ics.continuoustopk.datasets.DatasetConstants;
import gr.forth.ics.continuoustopk.datasets.ItemsIterator;
import gr.forth.ics.continuoustopk.datasets.QueriesIterator;
import gr.forth.ics.continuoustopk.index.QueryIndex;
import gr.forth.ics.continuoustopk.index.QueryIndexes;
import gr.forth.ics.continuoustopk.index.QueryUpdatedListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * This example is actually the same as the one in Experiments.java. The only
 * difference is that a number of listeners over several triggers are added.
 *
 * @author Vouzoukidou Nelly, email: nelly.vouzoukidou (at) gmail (dot) com
 */
public class MatchingInfo {
    public static void main(String[] args) throws FileNotFoundException {
        QueryIndex[] indexes = {
            QueryIndexes.sortedQueries(20),
            QueryIndexes.sortedBuckets(20, 4000),
            QueryIndexes.rectangularGrid(20, 1800),
            QueryIndexes.densityBuckets(3, 16),
            QueryIndexes.constantAngleOrdering(),
            QueryIndexes.colFilter()};
        
        QueryIndex index = indexes[1];
        
        QueriesIterator qit = QueriesIterator.mixedQueries();
        System.out.println("Adding queries...");
        int qCount = 0;
        while (qit.hasNext()) {
            index.add(qit.next());
            qCount++;
        }
        System.out.println("All " + qCount + " queries are added.");
        
        ItemsIterator itemsIter = ItemsIterator.pressItems();
        List<Item> items = new ArrayList<Item>();
        for (int i = 0; i < DatasetConstants.itemCount; i++) {
            items.add(itemsIter.next());
        }
        
        for (int i = 0; i < DatasetConstants.warmUp; i++) {
            Item item = items.get(i);
            index.match(item);
        }
        
        // Change:
        // *Important*: We only add the listeners after the warmup. Otherwise,
        // inforfmation on the warmup will also be considered. 
        // *********************************************************************
        // This listener will count the total number of queries that have been 
        // updated by any item
        index.addListener(QueryUpdatedListener.counter);
        // This listener will count number of distinct queries that have been 
        // updated by any item
        index.addListener(QueryUpdatedListener.setCounter);
        // A user defined listener
        index.addListener(new QueryUpdatedListener() {
            @Override
            public void onQueryUpdate(Query query, Item item, Term term, int skyband) {
                // Such a System.out.println is actually very noisy. 
                // Put in comments to turn it off or do something different.
                System.out.println("Query has been updated! " + query + " by " + item);
            }

            @Override
            public int count() {
                // For most of the cases will not need to do anything here
                return 0;
            }
        });
        long time = -System.currentTimeMillis();
        for (int i = DatasetConstants.warmUp; i < DatasetConstants.itemCount; i++) {
            Item item = items.get(i);
            index.match(item);
        }
        time += System.currentTimeMillis();
        System.out.println("time " + time);
        
        // Now that the test is over we want to print the information collected
        // by the QueryUpdatedListener.counter and QueryUpdatedListener.setCounter:
        System.out.println("Total updates: " + QueryUpdatedListener.counter.count());
        System.out.println("Total distinct updates: " + QueryUpdatedListener.setCounter.count());
    }
}
